var app = getApp()
//常量数据配置
var ConstantData = require('../../../../common/constant_data.js')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    isManage: '',
    phone: '',
    leseList: [],
    pageNum: 1,
    numPerPage: 10
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    console.log('optinons===', options)
    this.setData({
      phone: options.phone?options.phone:'',
      key: options.key?options.key:'',
      isManage: options.isManage
    })
    if (options.phone) {
      this.getApgeLeaseRecord()
    }
    if (options.isManage) {
      this.getuserList()
    }
  },
  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    var that = this;
    if (that.data.loading && !that.data.loadingComplete) {
      that.setData({
        pageNum: that.data.pageNum + 1, //每次触发上拉事件，把searchPageNum+1  
      });
      that.getApgeLeaseRecord();
    }
  },
  /**
   * 查看护工的租赁记录
   */
  getApgeLeaseRecord: function () {
    var that = this
    var phone = this.data.phone

    app.executeRequest(function (header) {

      wx.request({
        url: ConstantData.CONSTATNT_DATA.BASE_URL + '/time_card_patient/lease_rcord',
        header: header,
        data: {
          key: that.data.key,
          phone: phone,
          numPerPage: that.data.numPerPage,
          pageNum: that.data.pageNum
        },
        success: function (res) {
          var result = res.data
          var rst = result.result
          if (rst == 0) {
            //获取成功
            var oldList = that.data.leseList
            var newList = oldList.concat(res.data.data.data)
            if (res.data.data.pageNum * that.data.numPerPage >= res.data.data.total) {
              that.setData({
                loading: false,
                loadingComplete: true,
                leseList: newList
              });
            } else {
              that.setData({
                loading: true,
                loadingComplete: false,
                leseList: newList
              });
            }
          } else {
            wx.showToast({
              title: result.description,
            })
          }
        },
        fail: function () {
          wx.showToast({
            title: '稍后重试',
          })
        }
      })

    })

  },
  //管理员权限时获取人员列表
  getuserList() {
    var that = this

    app.executeRequest(function (header) {

      wx.request({
        url: ConstantData.CONSTATNT_DATA.BASE_URL + '/time_card_patient/careworkers',
        header: header,
        data: {
          key: ''
        },
        success: function (res) {
          var result = res.data
          var rst = result.result
          if (rst == 0) {
            result.data.forEach((item) => {
              item['name'] = item.remarks
              item['userId'] = item.id
            })
            result.data.unshift =result.data.unshift({
              name: '全部人员',
              userId: ''
            }) 
            //获取成功
            that.setData({
              carerWorkerList: result.data,
            })
          }
        },
        fail: function (res) {
          wx.showToast({
            title: '稍后重试',
          })
        }
      })

    })
  },
  //选择的人员id
  getUserId(e) {
    console.log('uear', e)
  },
  //选择的日期区间
  getDateIn(e) {
    console.log('fatedee', e)
  }

})